{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a5954e9-4866-4916-b11d-6ef8f9163c9d",
   "metadata": {},
   "source": [
    "# 第十二节、时间序列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe4500cf-f6a0-4e21-9ef3-dc1956b5e4b7",
   "metadata": {},
   "source": [
    "## （1）时间戳操作"
   ]
  },
  {
   "cell_type": "code",
   "id": "53e21308-8c68-43a4-81d7-de8f9d17e459",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.370222Z",
     "start_time": "2025-06-20T06:21:37.364173Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "id": "1400afd3-23ac-4021-8551-53490aaf0c4e",
   "metadata": {},
   "source": [
    "创建方法"
   ]
  },
  {
   "cell_type": "code",
   "id": "d7e39698-9f11-478a-93f3-c92ed3bba817",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.429700Z",
     "start_time": "2025-06-20T06:21:37.405417Z"
    }
   },
   "source": [
    "# 创建一个时间戳\n",
    "pd.Timestamp('2020-8-24 12')  # 时刻数据"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-08-24 12:00:00')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "id": "7549b6fb-d264-49ae-95ea-3ad5441139cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.458904Z",
     "start_time": "2025-06-20T06:21:37.452417Z"
    }
   },
   "source": [
    "# 创建一个时间范围数据\n",
    "pd.Period('2020-8-24', freq='M')  # 时期数据"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2020-08', 'M')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "id": "11b9bc21-2817-4ab1-812e-c41d7366cbf1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.477897Z",
     "start_time": "2025-06-20T06:21:37.468416Z"
    }
   },
   "source": [
    "# 批量创建时刻数据\n",
    "index = pd.date_range('2020.08.24', periods=5, freq='ME')\n",
    "index"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30',\n",
       "               '2020-12-31'],\n",
       "              dtype='datetime64[ns]', freq='ME')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "id": "175888ce-5d3a-49d5-9681-76643c478039",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.536375Z",
     "start_time": "2025-06-20T06:21:37.515698Z"
    }
   },
   "source": [
    "# 批量创建时期数据\n",
    "pd.period_range('2020.08.24', periods=5, freq='M')"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2020-08', '2020-09', '2020-10', '2020-11', '2020-12'], dtype='period[M]')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "id": "d93d83bd-74d2-4d21-86d0-df51295dafe6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.566289Z",
     "start_time": "2025-06-20T06:21:37.554746Z"
    }
   },
   "source": [
    "# 时间戳索引Series\n",
    "pd.Series(np.random.randint(0, 10, size=5), index=index)  # 时间戳索引Series"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-31    5\n",
       "2020-09-30    3\n",
       "2020-10-31    2\n",
       "2020-11-30    2\n",
       "2020-12-31    1\n",
       "Freq: ME, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "id": "e0d556cf-1500-4674-a29d-e3764cb80645",
   "metadata": {},
   "source": [
    "转换方法"
   ]
  },
  {
   "cell_type": "code",
   "id": "706f6a89-fc39-49d3-b070-81a085a70035",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.597225Z",
     "start_time": "2025-06-20T06:21:37.581796Z"
    }
   },
   "source": [
    "# 将日期转换成pandas的日期数据\n",
    "pd.to_datetime(\n",
    "    ['2020.8.24', '2020-8-24', '24/8/2020', '2020/08/24'],\n",
    "    format='mixed'  # 如果不指定这个参数，就没有办法处理混合日期格式\n",
    ")"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-24', '2020-08-24', '2020-08-24', '2020-08-24'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "id": "d8209150-5ebf-4d04-8594-083d8d7cf0c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.632910Z",
     "start_time": "2025-06-20T06:21:37.625788Z"
    }
   },
   "source": [
    "# 将时间戳转换为pandas时间对象\n",
    "dt = pd.to_datetime([1598582232], unit='s')  # unit='s' 这个一定要加，时间单位是秒\n",
    "dt"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-28 02:37:12'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "id": "2487f38a-0877-4dc2-92f7-3caf59a4476f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.682747Z",
     "start_time": "2025-06-20T06:21:37.674918Z"
    }
   },
   "source": [
    "# 变换时区\n",
    "dt + pd.DateOffset(hours=8)  # 东八区"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-28 10:37:12'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "id": "4d0d6ac0-5322-4926-b36a-54f8c385f19f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.714307Z",
     "start_time": "2025-06-20T06:21:37.706542Z"
    }
   },
   "source": [
    "# 变换日期\n",
    "dt + pd.DateOffset(days=100)  # 100天之后"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-12-06 02:37:12'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "id": "254589b8-a451-4bb4-ab35-74236872da15",
   "metadata": {},
   "source": [
    "## （2）时间戳索引"
   ]
  },
  {
   "cell_type": "code",
   "id": "4f8612e6-8de3-4b09-9f43-53a7884f14c1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.728639Z",
     "start_time": "2025-06-20T06:21:37.723329Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "id": "31fffb00-469d-497a-9be8-297e2d9644f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.748272Z",
     "start_time": "2025-06-20T06:21:37.741655Z"
    }
   },
   "source": [
    "index = pd.date_range('2020-8-24', periods=200, freq='D')\n",
    "ts = pd.Series(data=range(len(index)), index=index)"
   ],
   "outputs": [],
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "id": "c92ae673-5e46-4710-ac80-ca865bf125ca",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.762937Z",
     "start_time": "2025-06-20T06:21:37.755277Z"
    }
   },
   "source": [
    "ts"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-24      0\n",
       "2020-08-25      1\n",
       "2020-08-26      2\n",
       "2020-08-27      3\n",
       "2020-08-28      4\n",
       "             ... \n",
       "2021-03-07    195\n",
       "2021-03-08    196\n",
       "2021-03-09    197\n",
       "2021-03-10    198\n",
       "2021-03-11    199\n",
       "Freq: D, Length: 200, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "id": "ef3b0174-99c6-4caf-9a60-b06c55572a6d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.783207Z",
     "start_time": "2025-06-20T06:21:37.775009Z"
    }
   },
   "source": [
    "# str类型索引\n",
    "ts['2020-8-30']"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(6)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "id": "dd753305-fda1-47d7-97e4-09d19bc5be26",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.811675Z",
     "start_time": "2025-06-20T06:21:37.803162Z"
    }
   },
   "source": [
    "# 日期切片\n",
    "ts['2020-8-24': '2020-9-3']"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-24     0\n",
       "2020-08-25     1\n",
       "2020-08-26     2\n",
       "2020-08-27     3\n",
       "2020-08-28     4\n",
       "2020-08-29     5\n",
       "2020-08-30     6\n",
       "2020-08-31     7\n",
       "2020-09-01     8\n",
       "2020-09-02     9\n",
       "2020-09-03    10\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "id": "746bca2f-4f4b-4af9-86b3-0f5e3882754f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.845069Z",
     "start_time": "2025-06-20T06:21:37.836815Z"
    }
   },
   "source": [
    "# 传入年月，返回这个月所有的数据\n",
    "ts['2020-8']"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-24    0\n",
       "2020-08-25    1\n",
       "2020-08-26    2\n",
       "2020-08-27    3\n",
       "2020-08-28    4\n",
       "2020-08-29    5\n",
       "2020-08-30    6\n",
       "2020-08-31    7\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "id": "31fdf39e-b783-42a0-8e6a-daf507423e48",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.880244Z",
     "start_time": "2025-06-20T06:21:37.868213Z"
    }
   },
   "source": [
    "# 传入年，返回这一年所有的数据\n",
    "ts['2021']"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    130\n",
       "2021-01-02    131\n",
       "2021-01-03    132\n",
       "2021-01-04    133\n",
       "2021-01-05    134\n",
       "             ... \n",
       "2021-03-07    195\n",
       "2021-03-08    196\n",
       "2021-03-09    197\n",
       "2021-03-10    198\n",
       "2021-03-11    199\n",
       "Freq: D, Length: 70, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "id": "0ad8efc1-ec64-42f8-8796-a70f99c1413b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.901062Z",
     "start_time": "2025-06-20T06:21:37.892330Z"
    }
   },
   "source": [
    "# 如果我只知道时间戳，也可以将时间戳当做索引传入\n",
    "ts[pd.Timestamp('2020-8-30')]"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(6)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "id": "cd4e75d3-7dcf-4ada-a9dd-d1e9a941f280",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.940790Z",
     "start_time": "2025-06-20T06:21:37.933142Z"
    }
   },
   "source": [
    "# 把日期字符串，转换为时间戳\n",
    "pd.Timestamp('2020-8-30')"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-08-30 00:00:00')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "id": "fbd08106-b622-4994-8879-c6e54fa91352",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.963327Z",
     "start_time": "2025-06-20T06:21:37.955801Z"
    }
   },
   "source": [
    "# 时间戳切片\n",
    "ts[pd.Timestamp('2020-8-24'): pd.Timestamp('2020-8-30')]"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-24    0\n",
       "2020-08-25    1\n",
       "2020-08-26    2\n",
       "2020-08-27    3\n",
       "2020-08-28    4\n",
       "2020-08-29    5\n",
       "2020-08-30    6\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "id": "5a83c499-e7b5-4498-baf5-fb2b7e754729",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:37.987148Z",
     "start_time": "2025-06-20T06:21:37.978491Z"
    }
   },
   "source": [
    "# 时间范围进行索引\n",
    "ts[pd.date_range('2020-8-24', periods=10, freq='D')]  # periods=10时间间隔10天，freq='D'表示天为单位"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020-08-24    0\n",
       "2020-08-25    1\n",
       "2020-08-26    2\n",
       "2020-08-27    3\n",
       "2020-08-28    4\n",
       "2020-08-29    5\n",
       "2020-08-30    6\n",
       "2020-08-31    7\n",
       "2020-09-01    8\n",
       "2020-09-02    9\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "id": "bb62d9d8-af46-4331-bfd5-e80629d56d73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:38.021425Z",
     "start_time": "2025-06-20T06:21:38.014055Z"
    }
   },
   "source": [
    "# 时间戳索引属性\n",
    "ts.index"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27',\n",
       "               '2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31',\n",
       "               '2020-09-01', '2020-09-02',\n",
       "               ...\n",
       "               '2021-03-02', '2021-03-03', '2021-03-04', '2021-03-05',\n",
       "               '2021-03-06', '2021-03-07', '2021-03-08', '2021-03-09',\n",
       "               '2021-03-10', '2021-03-11'],\n",
       "              dtype='datetime64[ns]', length=200, freq='D')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "id": "6640c2d3-96e0-47ae-a42b-1af9011c9634",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:38.052433Z",
     "start_time": "2025-06-20T06:21:38.045611Z"
    }
   },
   "source": [
    "# 获取年份\n",
    "ts.index.year"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,\n",
       "       ...\n",
       "       2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021],\n",
       "      dtype='int32', length=200)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "id": "a12098fd-ad60-42ef-857c-addc774fa6a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:38.084894Z",
     "start_time": "2025-06-20T06:21:38.075831Z"
    }
   },
   "source": [
    "# 获取星期\n",
    "ts.index.dayofweek"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([0, 1, 2, 3, 4, 5, 6, 0, 1, 2,\n",
       "       ...\n",
       "       1, 2, 3, 4, 5, 6, 0, 1, 2, 3],\n",
       "      dtype='int32', length=200)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "id": "85ae73de-eccc-4a48-9e58-287a0134b5fc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:38.143680Z",
     "start_time": "2025-06-20T06:21:38.136775Z"
    }
   },
   "source": [
    "# 一年中的第今天\n",
    "ts.index.day_of_year"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([237, 238, 239, 240, 241, 242, 243, 244, 245, 246,\n",
       "       ...\n",
       "        61,  62,  63,  64,  65,  66,  67,  68,  69,  70],\n",
       "      dtype='int32', length=200)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "id": "574bbb7c-faf7-402b-a610-797197ae27f4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-20T06:21:38.181080Z",
     "start_time": "2025-06-20T06:21:38.175425Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
